"""
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
"""
"""仍然用dict保存数组元素出现的位置，两种情况下更新"""

class Solution(object):
    def containsDuplicate2(self,nums,k):
        dic = dict()
        for index,value in enumerate(nums):
            if value in dic and  index - dic[value] <= k:
                return True
            dic[value] = index
        return False